package de.ba_rm.java.password.checker.view;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;

import javax.swing.JPanel;

/**
 * GradientPanel is a class with a gradient background, put your non-opaque
 * objects over it and enjoy.
 * <br>
 * Special thanks to: 
 * @author Mohammad Abou-Basha
 * 
 * @see
 * <br>
 * <a href="http://aboubasha.blogspot.de/2007/06/jpanel-with-gradient-background.html">
 * http://aboubasha.blogspot.de/2007/06/jpanel-with-gradient-background.html
 * </a>
 */
public class GradientPanel extends JPanel {

	/** @see java.io.Serializable */
	private static final long serialVersionUID = 4214423592628867507L;

	private Color leftColor;

	private Color rightColor;
	
	private int entropy;

	public GradientPanel() {
		this(Color.RED, Color.GREEN);
	}

	public GradientPanel(final Color leftColor, final Color rightColor) {
		super();
		this.leftColor = leftColor;
		this.rightColor = rightColor;
	}

	@Override
	protected void paintComponent(final Graphics graphics) {
		super.paintComponent(graphics);
		final Point start = new Point(0, 0);
		final int width = (this.entropy<getWidth()) ? this.entropy*2 : getWidth();
		final Point end = new Point(getWidth(), getHeight());
		final GradientPaint gradientPaint = new GradientPaint(start, leftColor, end, rightColor);
		
		if (graphics instanceof Graphics2D) {
			final Graphics2D graphics2D = (Graphics2D) graphics;
			graphics2D.setPaint(gradientPaint);
			graphics2D.fillRect(start.x, start.y, width, end.y);
		}
	}
	
	public void setEntropy(int entropy) {
		this.entropy = entropy;
		paint(getGraphics());
	}
}
